package com.yuuzy.tankgame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;
//为了让panel不停的重绘子弹，需要将Mypanel实现Runnable，当做一个线程使用
public class MyPanel extends JPanel implements KeyListener,Runnable {
	//定义我方坦克
	MyTank myTank;
	//定义敌人坦克，放入到Vector（线程安全）
	Vector<EnemyTank> enemyTanks = new Vector<>();
	int enemyTankSize = 3;
	//坦克大战的绘图区域
	public MyPanel() {
		myTank = new MyTank(100, 100);//初始化己方坦克
		myTank.setSpeed(5);
		//初始化敌方坦克
		for (int i = 0;i < enemyTankSize;i++){
			EnemyTank enemyTank = new EnemyTank(100 * (i + 1), 0);
			enemyTank.setDirection(2);
			enemyTanks.add(enemyTank);
		}
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		g.fillRect(0, 0, 1000, 750);//填充矩形，默认黑色

		//画出我方坦克
		drawTank(myTank.getX(),myTank.getY(),g,myTank.getDirection(),0);

		//画出我方坦克射击的子弹
		if (myTank.shot != null && myTank.shot.isLive){
			g.fill3DRect(myTank.shot.x,myTank.shot.y,3,3,false);
		}
		//画出敌方坦克
		for (int i = 0; i < enemyTanks.size(); i++) {
			//取出坦克
			EnemyTank enemyTank = enemyTanks.get(i);
			drawTank(enemyTank.getX(),enemyTank.getY(),g,enemyTank.getDirection(),1);
		}
	}

	/**
	 * @param x         坦克的左上角x坐标
	 * @param y         坦克的左上角y坐标
	 * @param g         画笔
	 * @param direction 坦克方向（上下左右）
	 * @param type      坦克类型
	 */
	public void drawTank(int x, int y, Graphics g, int direction, int type) {
		switch (type) {
			case 0: {
				//我方坦克
				g.setColor(Color.cyan);
				break;
			}
			case 1: {
				g.setColor(Color.yellow);
				break;
			}
		}
		//根据坦克方向，来绘制坦克，direct：0表示向上，1表示向右，2表示向下，3表示向左
		switch (direction){
			case 0:
				//向上
				g.fill3DRect(x,y,10,60,false);//画出坦克左边轮子
				g.fill3DRect(x + 30,y,10,60,false);//画出坦克右边轮子
				g.fill3DRect(x + 10,y + 10,20,40,false);//画出坦克盖子
				g.fillOval(x + 10,y + 20,20,20);//画出坦克圆盖
				g.drawLine(x + 20,y,x + 20,y + 30);//画出炮筒
				break;
			case 1:
				//向右
				g.fill3DRect(x,y,60,10,false);//画出坦克上边轮子
				g.fill3DRect(x,y + 30,60,10,false);//画出坦克下边轮子
				g.fill3DRect(x + 10,y + 10,40,20,false);//画出坦克盖子
				g.fillOval(x + 20,y + 10,20,20);//画出坦克圆盖
				g.drawLine(x + 30,y + 20,x + 60,y + 20);//画出炮筒
				break;
			case 2:
				//向下
				g.fill3DRect(x,y,10,60,false);//画出坦克左边轮子
				g.fill3DRect(x + 30,y,10,60,false);//画出坦克右边轮子
				g.fill3DRect(x + 10,y + 10,20,40,false);//画出坦克盖子
				g.fillOval(x + 10,y + 20,20,20);//画出坦克圆盖
				g.drawLine(x + 20,y + 60,x + 20,y + 30);//画出炮筒
				break;
			case 3:
				//向左
				g.fill3DRect(x,y,60,10,false);//画出坦克上边轮子
				g.fill3DRect(x,y + 30,60,10,false);//画出坦克下边轮子
				g.fill3DRect(x + 10,y + 10,40,20,false);//画出坦克盖子
				g.fillOval(x + 20,y + 10,20,20);//画出坦克圆盖
				g.drawLine(x + 30,y + 20,x,y + 20);//画出炮筒
				break;
			default:
				System.out.println("暂时没有处理");
		}
	}

	@Override
	public void keyTyped(KeyEvent e) {

	}

	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_W){
			//按下w键
			myTank.setDirection(0);
			myTank.moveUp();
		}
		else if (e.getKeyCode() == KeyEvent.VK_D){
			//按下d键
			myTank.setDirection(1);
			myTank.moveRight();
		}
		else if (e.getKeyCode() == KeyEvent.VK_S){
			//按下s键
			myTank.setDirection(2);
			myTank.moveDown();
		}
		else if (e.getKeyCode() == KeyEvent.VK_A){
			//按下a键
			myTank.setDirection(3);
			myTank.moveLeft();

		}
		//如果用户按下J键，就发射
		if (e.getKeyCode() == KeyEvent.VK_J) {
			myTank.shotEnemyTank();
		}
		this.repaint();
	}

	@Override
	public void keyReleased(KeyEvent e) {

	}

	@Override
	public void run() {//每隔100ms重绘区域，刷新绘图区域，子弹就移动
		while (true){
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				throw new RuntimeException(e);
			}
			this.repaint();
		}

	}
}
